package com.success.application.user.activity;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.reflect.TypeToken;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseDetailActivity;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.myapplication.MainActivity;
import com.success.application.myapplication.R;
import com.success.application.user.AccountHelper;
import com.success.application.user.bean.User;
import com.success.application.util.DialogHelper;
import com.success.application.widget.SimplexToast;
import com.success.application.widget.TDevice;

import java.lang.reflect.Type;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/2/27.
 */

public class RegisterActivity extends BaseDetailActivity<Map> {
    private Dialog dialog;
    //登录名
    @Bind(R.id.et_register_username)
    EditText musername;
    //密码
    @Bind(R.id.et_register_pwd)
    EditText mpwd;
    @Override
    protected Type getType() {
        return new TypeToken<Result<Map>>() {
        }.getType();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main_register;
    }

    @Override
    protected int getTitleRes() {
        return R.string.main_register_user;
    }
    /**
     * 用户注册
     */
    @OnClick(R.id.bt_register_submit)
    void onClickRegisterSubmit() {
        registerRequest();
        dialog = DialogHelper.getProgressDialog(RegisterActivity.this, "注册中");
        dialog.show();
    }

    @Override
    protected void initData() {
        super.initData();
        //用户名校验
        musername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus){
                    //获得焦点处理
                }
                else {
                    //失去焦点处理
                    String tempUsername = musername.getText().toString().trim();
                    requestYhm(tempUsername);
                }
            }
        });
    }

    private void requestYhm(String tempUsername) {
        Params params = new Params();
        params.put("User_yhm", tempUsername);
        try {
            YuanbaoApi.checkYhm(params, mHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void registerRequest() {
        String tempUsername = musername.getText().toString().trim();
        String tempPwd = mpwd.getText().toString().trim();
        if (!TextUtils.isEmpty(tempPwd) && !TextUtils.isEmpty(tempUsername)) {
            //登录成功,请求数据进入用户个人中心页面
            if (TDevice.hasInternet()) {
                requestRegister(tempUsername, tempPwd);
            } else {
                SimplexToast.show(getApplicationContext(), "网络错误");
            }
        } else {
            SimplexToast.show(getApplicationContext(), "请输入正确的手机号");
        }
    }

    private void requestRegister(String tempUsername, String tempPwd) {
        Params params = new Params();
        params.put("model.User_yhm", tempUsername);
        params.put("model.User_mm", tempPwd);
        try {
            YuanbaoApi.getUserRegister(params, mHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void setData(Result<Map> result) {
        if (dialog != null) {
            dialog.dismiss();
        }
        //获取用户登录成功之后的信息
        Map mMap = result.getData();

        User user = new User();
        //系统用户的字段
        if(mMap.get("id")!=null){
            user.setId(mMap.get("id").toString());
        }
        if(mMap.get("User_yhm")!=null){
            user.setUser_yhm(mMap.get("User_yhm").toString());
        }
        if(mMap.get("User_mm")!=null){
            user.setUser_mm(mMap.get("User_mm").toString());
        }
        if(mMap.get("User_nc")!=null){
            user.setUser_nc(mMap.get("User_nc").toString());
        }
        if(mMap.get("User_dh")!=null){
            user.setUser_dh(mMap.get("User_dh").toString());
        }
        if(mMap.get("User_qq")!=null){
            user.setUser_qq(mMap.get("User_qq").toString());
        }
        if(mMap.get("User_nl")!=null){
            user.setUser_nl(mMap.get("User_nl").toString());
        }
        if(mMap.get("User_email")!=null){
            user.setUser_email(mMap.get("User_email").toString());
        }
        if(mMap.get("User_xx")!=null){
            user.setUser_xx(mMap.get("User_xx").toString());
        }
        if(mMap.get("User_tx")!=null){
            user.setUser_tx(mMap.get("User_tx").toString());
        }
        if(mMap.get("User_xymc")!=null){
            user.setUser_xymc(mMap.get("User_xymc").toString());
        }
        if(mMap.get("User_xmc")!=null){
            user.setUser_xmc(mMap.get("User_xmc").toString());
        }
        if(mMap.get("User_bjmc")!=null){
            user.setUser_bjmc(mMap.get("User_bjmc").toString());
        }
        if(mMap.get("User_js")!=null){
            user.setUser_js(mMap.get("User_js").toString());
        }
        if(mMap.get("User_djrq")!=null){
            user.setUser_djrq(mMap.get("User_djrq").toString());
        }
        if(mMap.get("User_bz")!=null){
            user.setUser_bz(mMap.get("User_bz").toString());
        }
        if(mMap.get("User_zt")!=null){
            user.setUser_zt(mMap.get("User_zt").toString());
        }
        if(mMap.get("User_csnyr")!=null){
            user.setUser_csnyr(mMap.get("User_csnyr").toString());
        }
        if(mMap.get("User_role")!=null){
            user.setUser_role(mMap.get("User_role").toString());
        }
        AccountHelper.updateUserCache(user);
//        Intent getIntent = getIntent();
//        String login = getIntent.getStringExtra("loginactivity");
//        if(login != null){
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivityForResult(intent, 1);
        finish();
//        }else{
//            setResult(RESULT_OK, getIntent);
//            finish();
//        }
    }
}
